﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace BPMS.Utility.Streams
{
    public static class StreamUtility
    {
        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static MemoryStream DeflateCompress(Stream stream)
        {
            MemoryStream memoryStream = new MemoryStream();
            using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress))
            {
                stream.CopyTo(deflateStream);
            }
            return memoryStream;
        }

        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static MemoryStream DeflateDecompress(Stream stream)
        {
            MemoryStream memoryStream = new MemoryStream();
            using (DeflateStream deflateStream = new DeflateStream(stream, CompressionMode.Decompress))
            {
                deflateStream.CopyTo(memoryStream);
            }
            return memoryStream;
        }

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static MemoryStream GZipCompress(Stream stream)
        {
            MemoryStream memoryStream = new MemoryStream();
            using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress))
            {
                stream.CopyTo(gZipStream);
            }
            return memoryStream;
        }

        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static MemoryStream GZipDecompress(Stream stream)
        {
            MemoryStream memoryStream = new MemoryStream();
            using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Decompress))
            {
                gZipStream.CopyTo(memoryStream);
            }
            return memoryStream;
        }
    }
}
